package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Employee;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.FlavorMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Set;

@Service
public class DishServiceImpl implements DishService {


    @Autowired
    private  DishMapper dishMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private FlavorMapper flavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Override
    public PageResult findDishPageQuery(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        List<DishVO> dishList =dishMapper.list(dishPageQueryDTO);

        Page<DishVO> page=(Page<DishVO>)dishList;
        PageResult pageResult=new PageResult(page.getTotal(), page.getResult());
        return pageResult;
    }

    @Override
    @Transactional
    public void addDish(DishDTO dishDTO) {
        Dish dish=new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.addDish(dish);
        Long dishid = dish.getId();

        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors!=null&&flavors.size()>0){
           flavors.forEach(dishFlavor -> dishFlavor.setDishId(dishid));
            flavorMapper.insert(flavors);
        }
        String key="dish_"+dishDTO.getCategoryId();
        cleanCache(key);



    }

    @Override
    public DishVO findDishById(Long id) {
        Dish dish = dishMapper.select(id);
        List<DishFlavor> byDishId = flavorMapper.findByDishId(id);
        DishVO dishVO= new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(byDishId);
        return dishVO;
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public void deleteDishById(Long[] ids) {
        //菜品如果起售就禁止删除
        for (Long id : ids) {
            Dish dish = dishMapper.select(id);
            if (dish.getStatus()== StatusConstant.ENABLE)
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
        }
        //菜品被套餐关联则不能被删除
        Long[] setmealIds = setmealDishMapper.getsetmealIdsByIds(ids);
           if (setmealIds!=null&&setmealIds.length>0){
                    throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
                }

//        for (Long id : ids) {
//           dishMapper.delete(id);
//            flavorMapper.deleteByDishID(id);
//        }

        dishMapper.delete(ids);
        flavorMapper.deleteByDishID(ids);

       cleanCache("dish_*");

    }

    @Override
    public void updateDish(Integer status, Long id) {
        Dish dish = Dish.builder()
                .status(status)
                .id(id)
                .build();
        dishMapper.updata(dish);

     cleanCache("dish_*");

    }

    @Override
    public void update(DishDTO dishDTO) {
        Dish dish= new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        //修改dish信息
        dishMapper.updata(dish);
        Long[] dishid={dish.getId()};
        //删除口味数据
        flavorMapper.deleteByDishID(dishid);
        //插入新的口味数据
        List<DishFlavor> flavors=dishDTO.getFlavors();
        if (flavors!=null&&flavors.size()>0){
            flavors.forEach(dishFlavor -> dishFlavor.setDishId(dishDTO.getId()));
            flavorMapper.insert(flavors);
        }
        cleanCache("dish_*");
    }

    @Override
    public List<Dish> findDishByCategoryId(Long categoryId) {

        return dishMapper.selectByCategoryId(categoryId);
    }

    private void cleanCache(String pattern){
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }
}
